package com.huaxinkaiye.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;



import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class TextUploadUtil {
	static String result = "";
	static String path = "";
	private Handler uihandler = null;
	static String TAG = "TestUploadUtil";

	public TextUploadUtil(Handler handler) {
		this.uihandler = handler;
	}

	public static String uploadFile(Context context,File file, String URL) {
		int responseCode = 0;
	  //URL="http://image.sinphyutaw.com/api/service/image/?action=upload&use=resume&bizid=101&cuid=6335006b8a86ed34c91e960f26fa2580&os=android&sv=1&ossv=19&sign=1&language=zh&curtime=1453628964413&stuss=9732d149c4e57463b55161a40008518a";
		HttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);// 
		
		HttpPost httppost = new HttpPost(URL);
		httppost.addHeader("auth", "0000;121a10c29912a912b12e61207122f1082e;A");	
			
		MultipartEntity mpEntity = new MultipartEntity(); // 文件传输	
		mpEntity.addPart("picture", new FileBody(file)); 
//		try {
//			mpEntity.addPart("action",new StringBody("upload"));
//			mpEntity.addPart("stuss",new StringBody(MySharedPreferences.getInstance(context).getString("stuss")));
//			mpEntity.addPart("cuid",new StringBody(Utils.getDeviceId(context)));
//			mpEntity.addPart("bizid",new StringBody("101"));
//			mpEntity.addPart("use",new StringBody("resume"));
//			mpEntity.addPart("sv",new StringBody(Utils.getVersionName(context)));
//			mpEntity.addPart("ossv",new StringBody(android.os.Build.VERSION.SDK));
//			mpEntity.addPart("sign",new StringBody(MySharedPreferences.getInstance(context).getString("sign")));
//			mpEntity.addPart("os",new StringBody("android"));
////			mpEntity.addPart("from",new StringBody("upload"));
////			mpEntity.addPart("longitude",new StringBody("upload"));
////			mpEntity.addPart("latitude",new StringBody("upload"));
//			mpEntity.addPart("curtime",new StringBody(System.currentTimeMillis()+""));	
//		} catch (UnsupportedEncodingException e2) {
			// TODO Auto-generated catch block
//			e2.printStackTrace();
//		}
		httppost.setEntity(mpEntity);
		
		Log.e("textUpliadUtil line38", "executing request " + httppost.getRequestLine());
		try {
			final HttpResponse response = httpclient.execute(httppost);
			final HttpEntity resEntity = response.getEntity();	
			
			responseCode = response.getStatusLine().getStatusCode();
			Log.i(TAG, "responseCode is :" + responseCode);
			
			if (resEntity != null) {
				result = EntityUtils.toString(resEntity, "utf-8");
				JSONObject p = null;
				try {
					p = new JSONObject(result);//把结果变成json对象
					path = (String) p.get("path");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (resEntity != null) {
				resEntity.consumeContent(); // 销毁内容
			}
			
		} catch (ClientProtocolException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			Log.e(TAG, "IoException:");
			e1.printStackTrace();
			
		}

		httpclient.getConnectionManager().shutdown();
//		TellConState();
		return result;
	}
//	private void TellConState() {
//		Message mesg = this.uihandler.obtainMessage();
//		mesg.what =1 ;
////		mesg.obj = str;
//		this.uihandler.sendMessage(mesg);
//
//	}

	 public static Bitmap getHttpBitmap(String data)  {  
	        Bitmap bitmap = null;  
	        try  
	        { 
	            //初始化一个URL对象  
	            URL url = new URL(data);  
	            //获得HTTPConnection网络连接对象  
	            HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
	            connection.setConnectTimeout(5*1000);  
	            connection.setDoInput(true);  
	            connection.connect();  
	            //得到输入流  
	            InputStream is = connection.getInputStream();  
	            Log.e("TAG", "*********inputstream**"+is);  
	            bitmap = BitmapFactory.decodeStream(is);  
	            Log.e("TAG", "*********bitmap****"+bitmap);  
	            //关闭输入流  
	            is.close();  
	            //关闭连接  
	            connection.disconnect();    
	        } catch (Exception e)  
	        {  
	        	 Log.e("iamgeurl--",data);
	            // TODO Auto-generated catch block  
	            e.printStackTrace();  
	        }  
	          
	        return bitmap;  
	    }  
}
